<template>


  <div class="component-upload-image">
    <div>
      <p style="font-size: 13px; color: #00000080; font-weight: 300">
        图片要求：宽高等于800px，大小1M内。
      </p>
    </div>
    <el-upload
      :action="uploadImgUrl"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      :on-progress="onProgressUpdate"
      name="file"
      :data="uploadData"
      :show-file-list="false"
      :on-change="changeUpload"
      style="display: inline-block; vertical-align: top"
    >


      <el-image v-if="!value" :src="value">
        <div slot="error" class="image-slot">
          <i class="el-icon-plus"/>
        </div>
      </el-image>
      <div v-else class="image">
        <el-image
          :src="value"
          style="width:100%;heigth:100%"
          fit="fill"
        />
        <div class="mask">
          <div class="actions">
            <span title="预览" @click.stop="dialogVisible = true">
              <i class="el-icon-zoom-in el-icon-mask"/>
            </span>
            <span title="移除" @click.stop="removeImage">
              <i class="el-icon-delete el-icon-mask"/>
            </span>
          </div>
        </div>
      </div>
    </el-upload>
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="70%"
      append-to-body
    >
      <img
        :src="value"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
  import {getToken} from "@/utils/auth";
  import {getUploadToken} from "@/api/monitor/file";

  export default {
    data() {
      return {
        dialogVisible: false,
        // uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
        // headers: {
        //   Authorization: "Bearer " + getToken(),
        // },

        uploadImgUrl: "https://upload-z2.qiniup.com",
        upload_domain: "",
        uploadData: {token: "", key: ""},
      };
    },
    props: {
      value: {
        type: String,
        default: "",
      },
    },
    methods: {
      removeImage() {
        this.$emit("input", "");
      },
      handleUploadSuccess(res) {
        this.$emit("input", this.upload_domain + res.key);
      },
      handleBeforeUpload() {
        let _self = this;
        return new Promise((resolve, reject) => {
          getUploadToken()
            .then((response) => {
              _self.uploadData.key = response.data.key;
              _self.uploadData.token = response.data.token;
              _self.upload_domain = response.data.domain;
              resolve(true);
            })
            .catch((err) => {
              console.log("出错了...", err);
              reject(false);
            });
        });
      },
      onProgressUpdate(event, file, fileList) {
        // this.loading = this.$loading({
        // lock: true,
        // text: "上传中",
        // background: "rgba(0, 0, 0, 0.7)",
        // });

        this.imgFlag = true;

        this.percent = Math.floor(event.percent);

      },
      handleUploadError() {
        this.$message({
          type: "error",
          message: "上传失败",
        });
        this.loading.close();
      },
      changeUpload(file, fileList) {
        this.isUpload = 1
        const isImage = file.raw.type === 'image/png' || file.raw.type === 'image/jpg' || file.raw.type === 'image/jpeg'
        const isLt5M = file.size < 1024 * 1024 * 1
        if (!isImage) {
          this.$message.error('上传只能是png,jpg,jpeg格式!')
          this.forbidUpload = false
        }
        if (!isLt5M) {
          this.$message.error('上传图片大小不能超过1M!')
          this.forbidUpload = false
        }

        if (isImage && isLt5M) {
          this.forbidUpload = true
          // this.uploadFile = file.raw || null
        } else {
          fileList.splice(-1, 1)
        }
      }
    },
    watch: {},
  };
</script>

<style scoped lang="scss">
  .el-image {
    height: 100%;
  }

  .image {
    position: relative;
    width: 100%;
    height: 100%;

    .mask {
      opacity: 0;
      position: absolute;
      top: 0;
      width: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      transition: all 0.3s;
    }

    &:hover .mask {
      opacity: 1;
    }
  }

  .el-upload-list__item {
    transition: none !important;
  }

  .el-icon-mask {
    font-size: 20px;
    margin-right: 5px;
  }

  .el-icon-plus {
    font-size: 20px;
  }
</style>
